package helper

import (
	"regexp"
	"strings"
)

var aArray = []string{"ấ", "ầ", "ẩ", "ẫ", "ậ", "Ấ", "Ầ", "Ẩ", "Ẫ", "Ậ",
	"ắ", "ằ", "ẳ", "ẵ", "ặ", "Ắ", "Ằ", "Ẳ", "Ẵ", "Ặ", "á", "à",
	"ả", "ã", "ạ", "â", "ă", "Á", "À", "Ả", "Ã", "Ạ", "Â", "Ă"}

var eArray = []string{"ế", "ề", "ể", "ễ", "ệ", "Ế", "Ề", "Ể", "Ễ", "Ệ",
	"é", "è", "ẻ", "ẽ", "ẹ", "ê", "É", "È", "Ẻ", "Ẽ", "Ẹ", "Ê"}

var iArray = []string{"í", "ì", "ỉ", "ĩ", "ị", "Í", "Ì", "Ỉ", "Ĩ", "Ị"}

var oArray = []string{"ố", "ồ", "ổ", "ỗ", "ộ", "Ố", "Ồ", "Ổ", "Ô", "Ộ",
	"ớ", "ờ", "ở", "ỡ", "ợ", "Ớ", "Ờ", "Ở", "Ỡ", "Ợ", "ó", "ò",
	"ỏ", "õ", "ọ", "ô", "ơ", "Ó", "Ò", "Ỏ", "Õ", "Ọ", "Ô", "Ơ"}

var uArray = []string{"ứ", "ừ", "ử", "ữ", "ự", "Ứ", "Ừ", "Ử", "Ữ", "Ự",
	"ú", "ù", "ủ", "ũ", "ụ", "ư", "Ú", "Ù", "Ủ", "Ũ", "Ụ", "Ư"}

var yArray = []string{"ý", "ỳ", "ỷ", "ỹ", "ỵ", "Ý", "Ỳ", "Ỷ", "Ỹ", "Ỵ"}

var dArray = []string{"đ", "Đ"}

func GenerateAlias(title string) (alias string) {
	/*
		Remove all vietnamese character
	*/
	for _, element := range aArray {
		title = strings.Replace(title, element, "a", -1)
	}
	for _, element := range eArray {
		title = strings.Replace(title, element, "e", -1)
	}
	for _, element := range iArray {
		title = strings.Replace(title, element, "i", -1)
	}
	for _, element := range oArray {
		title = strings.Replace(title, element, "o", -1)
	}
	for _, element := range uArray {
		title = strings.Replace(title, element, "u", -1)
	}
	for _, element := range yArray {
		title = strings.Replace(title, element, "y", -1)
	}
	for _, element := range dArray {
		title = strings.Replace(title, element, "d", -1)
	}

	/*
		remove special character
	*/
	re := regexp.MustCompile("[-\\!\"\\@\\#\\,\\.\\$%\\^&\\*\\(\\)_\\+\\=\\?\\;\\:\\~\\`\\{\\}\\[\\]\\|\\\\]+")
	title = re.ReplaceAllLiteralString(title, "")

	re = regexp.MustCompile("[\\'\\/]+")
	title = re.ReplaceAllLiteralString(title, "")

	re = regexp.MustCompile(" ")
	title = re.ReplaceAllLiteralString(title, "-")

	re = regexp.MustCompile("–")
	title = re.ReplaceAllLiteralString(title, "")

	re = regexp.MustCompile("“")
	title = re.ReplaceAllLiteralString(title, "")

	re = regexp.MustCompile("”")
	title = re.ReplaceAllLiteralString(title, "")

	/*
		remove end with "-" character like : halo-
	*/
	re = regexp.MustCompile("-$")
	title = re.ReplaceAllLiteralString(title, "")

	/*
		remove start with "-" character like : -halo
	*/
	re = regexp.MustCompile("^-")
	title = re.ReplaceAllLiteralString(title, "")

	/*
		remove many "-" together like : a---b
	*/
	re = regexp.MustCompile("[-]+")
	title = re.ReplaceAllLiteralString(title, "-")

	/*
		change to lower case
	*/
	title = strings.ToLower(title)
	alias = title
	return
}

func CheckRemove(str string) (boo bool) {
	boo = false
	str = strings.ToLower(str)

	remove := "theo,Nhưng,Vì,Có,Các,Vì thế,Chính vì thế,Thậm chí,Tuy nhiên,Vì vậy,Nhiều,Đừng,Tùy,Hiện nay,Tuỳ theo,Tuy vậy,sau,Nay,Thông,Thế mà,Bữa nay,Kế đến,Người,Chẳng,Trong,Chuyện,Không,Những,Chiếc,trước,source,chuyến,thường,nguồn,chúng"

	listRemove := strings.Split(remove, ",")

	for _, element := range listRemove {
		if strings.HasPrefix(str, strings.ToLower(element)) {
			boo = true
		}
	}

	if str == "Source" {
		boo = true
	}
	return boo
}

func GenerateTag(str string) (result string) {
	str = strings.TrimSpace(str)

	re := regexp.MustCompile("[ ]*[.]+[ ]*")
	str = re.ReplaceAllLiteralString(str, "daucham")

	re = regexp.MustCompile("[ ]+")
	str = re.ReplaceAllLiteralString(str, "-")

	re = regexp.MustCompile("[\\,\\=\\+.!@#$%^&*()_]+")
	str = re.ReplaceAllLiteralString(str, ".-")

	str = strings.Replace(str, "“", ".", -1)
	str = strings.Replace(str, "”", ".", -1)
	str = strings.Replace(str, "daucham", ".", -1)

	words := strings.Split(str, "-")
	var count int = 0
	var check string = ""
	var check_arr [5]string
	var s_word string = ""
	var s_check string = ""

	for i := 0; i < len(words)-1; i++ {

		var matcher01 bool
		var matcher02 bool
		var matcher03 bool
		strRegex01 := "^[A-ZĐÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝĂĨŨƠƯẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰ]+[\\w\\W]+"
		strRegex02 := "^[^.]+[.]+[\\w\\W]+"
		strRegex03 := "^[^.]+[.]+[\\w\\W]*"

		matcher01, _ = regexp.MatchString(strRegex01, words[i])
		matcher02, _ = regexp.MatchString(strRegex03, words[i])
		if matcher01 && !matcher02 && count <= 4 && i != 0 {

			var wk string
			matcher01, _ = regexp.MatchString(strRegex01, words[i+1])
			matcher02, _ = regexp.MatchString(strRegex02, words[i+1])
			matcher03, _ = regexp.MatchString(strRegex03, words[i])

			if i < len(words)-2 && matcher01 && !matcher02 && !matcher03 {
				words[i] = words[i] + " " + words[i+1]
				wk = words[i]
				i = i + 1
			}

			matcher01, _ = regexp.MatchString(strRegex01, words[i+1])
			matcher02, _ = regexp.MatchString(strRegex02, words[i+1])
			matcher03, _ = regexp.MatchString(strRegex03, words[i])

			if i < len(words)-2 && matcher01 && !matcher02 && !matcher03 {
				words[i] = words[i-1] + " " + words[i+1]
				wk = words[i]
				i = i + 1
			}

			matcher01, _ = regexp.MatchString(strRegex01, words[i+1])
			matcher02, _ = regexp.MatchString(strRegex02, words[i+1])
			matcher03, _ = regexp.MatchString(strRegex03, words[i])

			if i < len(words)-2 && matcher01 && !matcher02 && !matcher03 {
				words[i] = words[i-1] + " " + words[i+1]
				wk = words[i]
				i = i + 1
			}			
			
			check = ""

			if wk != "" {
				s_word = wk
			} else {
				s_word = words[i]
			}

			for j := 0; j < len(check_arr); j++ {
				if check_arr[j] != "" {
					s_check = strings.ToLower(check_arr[j])
				}
				if strings.ToLower(s_word) == s_check {
					check = "1"
				}
			}

			if CheckRemove(s_word) {
				check = "1"
			}

			if check == "" && len(s_word) >= 5 {
				if count == 0 {
					temp := strings.Replace(s_word, "[\\,\\=\\+ .!@#$%^&*()_-]+", " ", -1)
					temp = strings.Replace(temp, "“", "", -1)
					temp = strings.Replace(temp, ".", "", -1)
					result += strings.TrimSpace(temp)
				} else {
					temp := strings.Replace(s_word, "[\\,\\=\\+ .!@#$%^&*()_-]+", " ", -1)
					temp = strings.Replace(temp, "“", "", -1)
					temp = strings.Replace(temp, ".", "", -1)
					result += "," + strings.TrimSpace(temp)
				}
				check_arr[count] = s_word
				count++
			}

		}
		if count >= 6 {
			break
		}
	}
	return
}
